package com.ddwl.funds.dao;

import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.constant.OrderEnum;
import com.ddwl.common.dao.TkDao;
import com.ddwl.funds.dao.mapper.OrderWxPayMapper;
import com.ddwl.funds.dao.model.OrderWxPay;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * 微信支付流水表(OrderWxPay)数据处理
 *
 * @author zach
 * @since 2020-09-24 11:42:01
 */
@Repository
public class OrderWxPayDao  extends TkDao<OrderWxPayMapper, OrderWxPay> {

    /**
     * 获取正在支付的数据
     * @param orderPayId
     * @return
     */
    public OrderWxPay nowPay(String orderPayId){
        Example example = new Example(OrderWxPay.class);
        example.and().andEqualTo("outTradeNo",orderPayId)
                .andEqualTo("dflag", BaseEnum.DflagEnum.NOT_DELETE.getCode());
        example.setOrderByClause("gmt_created desc limit 1");
        List<OrderWxPay> orderWxPays = baseMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(orderWxPays)){
            return null;
        }
        return orderWxPays.get(0);
    }

    public OrderWxPay nowPay(String orderPayId,String appId){
        Example example = new Example(OrderWxPay.class);
        example.and().andEqualTo("outTradeNo",orderPayId)
                .andEqualTo("appid",appId)
                .andEqualTo("dflag", BaseEnum.DflagEnum.NOT_DELETE.getCode());
        example.setOrderByClause("gmt_created desc limit 1");
        List<OrderWxPay> orderWxPays = baseMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(orderWxPays)){
            return null;
        }
        return orderWxPays.get(0);
    }

    /**
     * 查询旧的支付数据
     * @return
     */
    public List<OrderWxPay> queryOldPay(){
        Example example = new Example(OrderWxPay.class);
        example.and().andEqualTo("status", OrderEnum.PayStatus.SUCCEED.getCode())
                .andIsNull("mchKey")
                .andEqualTo("dflag", BaseEnum.DflagEnum.NOT_DELETE.getCode());
        List<OrderWxPay> orderWxPays = baseMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(orderWxPays)){
            return null;
        }
        return orderWxPays;
    }



}